#!perl
use LWP::Simple;
use threads ('yield',
                 'stack_size' => 64*4096,
                 'exit' => 'threads_only',
                 'stringify');

open(OUT, '>status.log') or die $!;

while ($choice != 3){
print "\n\tMenu\n
--------------------\n
1. Enter playlist ID\n
2. Search for songs\n
3. Exit\n
Enter choice: ";

$choice = <STDIN>;print "\n";

&dlPlaylist() if $choice == 1;
&dlSearch() if $choice == 2;
$xml = "";
}

sub dlPlaylist {
	print "Enter a playlist ID: ";
	$playlistID = <STDIN>;
	chomp($playlistID);
	$rs = randString(9);
	$playlistURL = "http://www.seeqpod.com/api/music/getPlaylist?playlist_id=$playlistID&v=2&random=$rs";
	$xml = get $playlistURL;
	&findSongs();
}

sub dlSearch {
	print "Enter search terms: ";
	$search = <STDIN>;
	chomp($search);
	$rs = randString(9);
	print OUT "$rs\n";
	$searchURL = "http://www.seeqpod.com/api/seeq/search?rt=0&s=0&n=30&q=$search&rv=0&rm=1&rp=0&random=$rs";
	$xml = get $searchURL;
	&findSongs();
}

sub findSongs {
	$xml =~ /\<name\>(.*)\<\/name\>/;
	$playlistName = $1;

	#@artists = $xml =~ /\<\/mp3_url_id\>(.*?)\<creator\/\>|\<\/mp3_url_id\>.*?\<creator\>(.*?)\<\/creator\>/g;
	@songs = $xml =~ /\<title\>(.*?)\<\/title\>/g;
	@artists = $xml =~ /\<creator\>(.*?)\<\/creator\>|.*?\<creator\/\>/g;

	print "\n\t\tSongs\n----Enter the song numbers or the word all----\n";
	for($i = 0; $i < @songs; $i++){
		#$songN = $i + 1;
		print "".($i+1) . ". ";
		print @artists[$i] . " - " if (@artists[$i]);
		print @songs[$i] . "\n";
	}
	print "Which songs would you like: ";
	$choices = <STDIN>;
	chomp ($choices);

	@downloadIDs = $xml =~ /\<mp3_url_id\>(.*?)\<\/mp3_url_id\>/g;
	print "Downloading chosen songs in $playlistName...\n";

	print "----Save to where----\n";
	print "1. Directory the program is in\n";
	print "2. Enter directory\n";
	print "Enter choice: ";
	$dirChoice = <STDIN>;
	chomp $dirChoice;

	if ($dirChoice == 1){
	if ($0=~m#^(.*)\\#) {
	    $dir = "$1";
	} elsif ($0=~m#^(.*)/# ) {
	    $dir = "$1";
	} else  {`pwd` =~ /(.*)/;
    	$dir = "$1";
	}}
	if ($dirChoice == 2){
		print "\nEnter the directory to save files in: ";
		$dir = <STDIN>;
		chomp $dir;
	}
	print "\nSaving songs to $dir\n";
	
	if ($choices != "all"){
	@chosenSongs = split (/ /,$choices);
	for ($j = 0; $j < @chosenSongs; $j++){
		$temp = (@chosenSongs[$j]-1);
		threads->create (\&getSong, @downloadIDs[$temp], @songs[$temp])->detach();
	}}
	
	if ($choices == "all"){
	for ($j = 0; $j < @songs; $j++){
		threads->create (\&getSong, @downloadIDs[$j], @songs[$j])->detach();
	}
	}
}

sub getSong {
	$rs = &randString(9);
	print OUT "$rs\n";
	$dlURL = "http://www.seeqpod.com/api/youtube.com/results?mp3_url_id=@_[0]&sq=@_[0]&random=$rs";
	$dlSave = $dir . "\\" . @_[1] . ".mp3";
	$dlURL = get $dlURL;
	@head = head($dlURL);
	print OUT "$dlSave Size: " . @head[1] . "\n";
	$rc = getstore $dlURL, $dlSave;
	print (-s $dlSave);
	while (@head[1] != (-s $dlSave)){
		$curSize = -s $dlSave;
		print OUT "$curSize out of @head[1]\n";
	}
	print OUT "Finished saving: @_[1]...\n" if (is_success($rc));
	#print $rc . "\n";

}

sub randString
{
	my $length_of_randomstring=shift;# the length of 
			 # the random string to generate

	my @chars=('a'..'z','A'..'Z','0'..'9');
	my $random_string;
	foreach (1..$length_of_randomstring) 
	{
		# rand @chars will generate a random 
		# number between 0 and scalar @chars
		$random_string.=$chars[rand @chars];
	}
	return $random_string;
}


#2d40e9106a